package controller;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.Model;
import view.TestSpinner;
import view.View;

public class TestSkiller extends WindowAdapter implements ActionListener, ChangeListener {
	private Model data;
	private View gui;
	
	
	public TestSkiller(Container masterView) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			System.out.println("Couldnt load system look and feel");
		}
		
		data = new Model();
		gui = new View(this, data);
		
		data.addObserver(gui);
		
		gui.setVisible(true);
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("received " + e.getActionCommand());
		if (e.getSource() == gui.getChckbxSaveValues()) {
			data.user.storeValues = gui.getChckbxSaveValues().isSelected();
			if (!data.user.storeValues) {
				data.deleteSave();
			}
		} 
		else if (e.getActionCommand().equals("reset#all")) {
			data.resetAll();
		}
		else if (e.getActionCommand().equals("reset#skillung")) {
			data.resetSkillung();
		}
		else if (e.getActionCommand().equals("reset#stats")) {
			data.resetStats();
		}
		else if (e.getActionCommand().equals("reset#spezialist")) {
			data.resetSpezialist();
		}


	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if (!data.globalReset) {
			if (e.getSource() == gui.getSpinnerJobLevel()) {
				data.setJobLevel(gui.getSpinnerJobLevel().getValueInteger());
			} else if (e.getSource() == gui.getSpinnerUpgrade()) {
				data.setUpgrade(gui.getSpinnerUpgrade().getValueInteger());
			} else if (gui.isFromSkillSpinner(e.getSource())) {
				//TODO only accept when not triggered by notifyObservers
				data.setValues(gui.getSkillValues(),
						(TestSpinner) e.getSource());
			} else if (gui.isFromSpSpinner(e.getSource())) {
				data.setSpValues(gui.getSpValues(), (TestSpinner) e.getSource());
			}
		}
	}
	
	@Override
	public void windowClosing(WindowEvent e) {		
		if(data.user.storeValues) {
			data.serialize();
		}
	}
	
	public View getView() {
		return gui;
	}
}
